<template>
  <div class='subjects'>
  <el-card>
    <!--搜索 -->
    <el-row>
      <el-col :span="18">
        <el-form label-width="80px" inline size="small">
          <el-form-item label="学科名称">
            <el-input v-model="reqParams.subjectName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear()">清除</el-button>
            <el-button @click="filter()" type="primary">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-button style="float:right" type="success" icon="el-icon-edit" size="small" @click="openDialog()">添加学科</el-button>
      <el-col :span="6"></el-col>
    </el-row>
  <el-alert style="margin-bottom:15px" :title=" `数据一共 ${total} 条`" type="info" :closable="false" show-icon></el-alert>
  <el-table :data="subjects">
    <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
    <el-table-column label="学科名称" prop="subjectName"></el-table-column>
    <el-table-column label="创建者" prop="username"></el-table-column>
    <el-table-column label="创建日期">
          <template #default={row}>{{row.addDate|parseTimeByString}}</template>
    </el-table-column>
    <el-table-column label="前台是否显示">
          <template #default={row}>{{row.isFrontDisplay===1?'是':'否'}}</template>
    </el-table-column>
    <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
    <el-table-column label="标签" prop="tags"></el-table-column>
    <el-table-column label="题目数量" prop="totals"></el-table-column>
    <el-table-column label="操作" width="240px">
        <template #default={row}>
      <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科分类</el-button>
      <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科标签</el-button>
      <el-button type="text" @click="openDialog(row)">修改</el-button>
      <el-button type="text" @click="del(row)">删除</el-button>
        </template>
    </el-table-column>
  </el-table>
  <el-pagination class="pagger"
  background layout="prev, pager, next, sizes, jumper"
  :total="total"
  :page-size="reqParams.pagesize"
  :current-page="reqParams.page"
  @current-change="pager"
  @size-change="handleSizeChange"
  ></el-pagination>
  </el-card>
  <SubjectsAdd ref="subjectsadd" :data="focussubjects" @updateList="updateList()"></SubjectsAdd>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  name: 'subjects',
  components: { SubjectsAdd },
  data () {
    return {
      reqParams: {
        subjectName: null,
        page: 1,
        pagesize: 10
      },
      subjects: [],
      total: 0,
      focussubjects: {}
    }
  },
  // 初始化渲染
  created () {
    this.getList()
    console.log(this.$data)
  },
  methods: {
    // 获取列表数据
    async getList () {
      const { data } = await list(this.reqParams)
      console.log(data)
      this.subjects = data.items
      this.total = data.counts
    },
    pager (p) {
      this.reqParams.page = p
      this.getList()
    },
    handleSizeChange (size) {
      this.reqParams.page = 1
      this.reqParams.pagesize = size
      this.getList()
    },
    // 清除搜索框
    clear () {
      this.reqParams = {
        subjectName: '',
        page: 1,
        pagesize: 10
      }
    },
    // 搜索列表数据并渲染
    filter () {
      this.reqParams.page = 1
      this.getList()
    },
    async del (id) {
      await this.$confirm('将永久删除此学科，是否确认', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(id)
      this.$message.success('删除成功')
      this.getList()
    },
    openDialog (subject) {
      this.focussubjects = subject || {}
      console.log(this.focussubjects)
      this.$nextTick(() => {
        this.$refs.subjectsadd.open()
      })
    },
    updateList () {
      if (!this.focussubjects.id) {
        this.reqParams.page = 1
      }
      this.getList()
    },
    indexMethod (index) {
      console.log(index, 111)
      // 第二页为前面的页码*条数
      return index + 1 + (this.reqParams.page - 1) * this.reqParams.pagesize
    }

  }
}
</script>

<style scoped lang='scss'>
.subjects{
  padding: 15px;
  .pagger{
    margin-top: 15px;
    margin-bottom: 15px;
    float: right;
  }
}
</style>
